/***************************************************************************
 *   Copyright (C) 2004 by Richard Moore (rich@kde.org                     *
 *   Copyright (C) 2007 by Olaf Freyer                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/

#include "common/setup_before.h"

#ifdef WIN32

#include "console_output.h"
#include <windows.h>
#include <fcntl.h>
#include <io.h>
#include <iostream>
#include <fstream>
#include "common/setup_after.h"

namespace pvpgn
{

Console::Console(){
	initialised = false;
}

Console::~Console() throw () {
	if (initialised)
		FreeConsole();
}

void Console::RedirectIOToConsole() {
   const WORD MAX_CONSOLE_LINES = 500;
   const WORD CONSOLE_COLUMNS= 140;
   int hConHandle;
   long lStdHandle;
   std::FILE *fp;
   CONSOLE_SCREEN_BUFFER_INFO coninfo;

   if (initialised)
	   return;

   AllocConsole(); 
   GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
   coninfo.dwSize.X = CONSOLE_COLUMNS;
   coninfo.dwSize.Y = MAX_CONSOLE_LINES;
   SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
   
   lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
   hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
   fp = _fdopen(hConHandle, "r");
   *stdin = *fp;
   setvbuf( stdin, NULL, _IONBF, 0 );;

   lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
   hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
   fp = _fdopen(hConHandle, "w");
   *stdout = *fp;
   setvbuf( stdout, NULL, _IONBF, 0 );

   lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
   hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
   fp = _fdopen(hConHandle, "w");
   *stderr = *fp;
   setvbuf( stderr, NULL, _IONBF, 0 );

   std::ios::sync_with_stdio();
   initialised = true;
}

}

#endif
